package com.ziyia.fragmentandviewpager2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ViewPager2
        viewPager = findViewById(R.id.viewPager);

        // 创建自定义的适配器
        MyAdapter myAdapter = new MyAdapter();

        // 将适配器添加到我们的ViewPager2
        viewPager.setAdapter(myAdapter);

    }


    // 点击不同的按钮切换不同的界面
    public void xh(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                viewPager.setCurrentItem(0);
                break;

            case R.id.btn2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.btn3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.btn4:
                viewPager.setCurrentItem(3);
                break;
        }
    }
}